/*
 * @(#)DocumentMessageInfo.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.Utility;
/**
 * <p>
 * Class Name: DocumentMessageInfo.java
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-09-24
 */
public class DocumentMessageInfo extends AbstractMessageInfo {
  DocumentRetrievalManager docRetManager ;
  Integer objectid ;
  public DocumentMessageInfo()
  {    
  }
  public DocumentMessageInfo(Integer objectid) {
    this.objectid=objectid;
  }



  public void setOtherInfo() throws ApplicationException {

    docRetManager = new DocumentRetrievalManager(sessionContainer, dbConn);
    DmsDocument dmsDocument = docRetManager.getDocument(objectid);
    this.messageInfoBean.setFileName(dmsDocument.getDocumentName());
    this.messageInfoBean.setPicture(isPicture(dmsDocument.getDocumentName()));
    this.messageInfoBean.setDocumentID(objectid.toString());
    DmsDocument documentRoot = docRetManager.getRootFolderByRootID(dmsDocument.getRootID());
    this.messageInfoBean.setRootFolder(documentRoot);
    setInvitationMessage();
    handleInvitationTitleDesc();
  }
  /**
   * 
   */
  private void handleInvitationTitleDesc() {
    if (UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(this.alertSystemLog.getActionType())) {
      this.messageInfoBean.setInvitationTitle(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.title.cancel"));
      this.messageInfoBean.setInvitationDesc(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.desc.cancel"));
    }
    else if (UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(this.alertSystemLog.getActionType())) {
      this.messageInfoBean.setInvitationTitle(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.title.expiry"));
      this.messageInfoBean.setInvitationDesc(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.desc.expiry"));

    }
    else if (UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(this.alertSystemLog.getActionType())) {
      this.messageInfoBean.setInvitationTitle(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.title.update_due_date"));
      this.messageInfoBean.setInvitationDesc(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.desc.update_due_date"));

    }
    else if (UpdateAlert.INVITE_ACTION.equals(this.alertSystemLog.getActionType())
            ||UpdateAlert.DISCUSS_ACTION.equals(this.alertSystemLog.getActionType())) {
      this.messageInfoBean.setInvitationTitle(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.title.new"));
      this.messageInfoBean.setInvitationDesc(MessageResourcesFactory.getMessage(sessionContainer
          .getSessionLocale(), "message.document.invitation.desc.new", this.messageInfoBean
          .getSender(), this.messageInfoBean.getFileName()));
    }
  }
  /**
   * @throws ApplicationException
   */
  private void setInvitationMessage() throws ApplicationException {
    UpdateAlertTypeDAObject updateAlertTypeDAObject = new UpdateAlertTypeDAObject(sessionContainer,
        dbConn);
    UpdateAlertType updateAlertType;
    if(UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(this.alertSystemLog.getActionType())
        ||UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(this.alertSystemLog.getActionType())){
     updateAlertType = (UpdateAlertType) updateAlertTypeDAObject.getByupdateAlertTypeID(this.alertSystemLog.getRefObjectID());
      this.messageInfoBean.setMessage(updateAlertType.getMessage());
      if(!Utility.isEmpty(updateAlertType.getDueDate())){
      this.messageInfoBean.setDueDate(updateAlertType.getDueDate());
      }
      this.messageInfoBean.setSendDate(parseDate(updateAlertType.getCreateDate()));
      this.messageInfoBean.setCanShowDoc(false);
      this.messageInfoBean.setCanShowSentDate(true);
    }else{
      this.messageInfoBean.setCanShowSentDate(false);
      DocumentInvitationManager documentInvitationManager =
        new DocumentInvitationManager (sessionContainer,dbConn);
      if(UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(this.alertSystemLog.getActionType())){
        updateAlertType = (UpdateAlertType) updateAlertTypeDAObject.getByupdateAlertTypeID(this.alertSystemLog.getRefObjectID());
        this.messageInfoBean.setMessage(updateAlertType.getMessage());
        if(!Utility.isEmpty(updateAlertType.getDueDate())){
        this.messageInfoBean.setDueDate(updateAlertType.getDueDate());
        }
      }else{
      updateAlertType = (UpdateAlertType) updateAlertTypeDAObject.getByupdateAlertTypeID(this.alertSystemLog.getUpdateAlertTypeID(),null);
      }
      String status =documentInvitationManager.getDocumentInvitationStatus(updateAlertType);
      if(DocumentInvitationManager.STATUS_ACTIVE.equals(status)){
        this.messageInfoBean.setCanShowDoc(true);
      }else{
        this.messageInfoBean.setCanShowDoc(false);
      }
    }
  }
/**
 * 
 * @param date
 * @return
 */
  private String parseDate(Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy",sessionContainer.getSessionLocale());
    return formatter.format(date);
  }
  private boolean isPicture(String documentName) {
    List picTypeList = getPictureTypeList();
    String extensName ="";
    if(documentName.indexOf(".")>-1)
    {
      extensName= documentName.substring(documentName.lastIndexOf(".")+1).toLowerCase().trim();
    }
    if(!Utility.isEmpty(extensName)&&picTypeList.contains(extensName))
    {
      return true;
    }
    return false;
  }
  private List getPictureTypeList() {
    List picTypeList ;
    String pictypeStr = "gif,jpg,jpeg,jpe,jfif,png";
    picTypeList = Arrays.asList(pictypeStr.split(","));
    return picTypeList;
  }


}
